﻿using OpenTK.Graphics.OpenGL4;

namespace Painter3D.RenderSys.RenderObject
{
    /// <summary>
    /// 使用数组绘制模式的顶点数据
    /// </summary>
    class ArrayVertexData : VertexData
    {
        /// <summary>
        /// 描述该顶点数据期望的绘制图元模式
        /// </summary>
        readonly public PrimitiveType shapeType;
        /// <summary>
        /// 预期绘制的顶点数量
        /// </summary>
        protected int drawSize;

        /// <summary>
        /// 构造使用数组绘制的顶点数据对象，该对象只有位置信息
        /// </summary>
        /// <param name="position">顶点位置数据数组</param>
        /// <param name="type">图元类型</param>
        public ArrayVertexData(float[] position, PrimitiveType type = PrimitiveType.Triangles) : base(position)
        {
            shapeType = type;
            drawSize = position.Length / 3;
        }

        /// <summary>
        /// 构造使用数组绘制的顶点数据对象
        /// </summary>
        /// <param name="requests">数据请求数组</param>
        /// <param name="datas">数据，按照请求排列，约定第一个为位置数据</param>
        /// <param name="type">图元类型</param>
        public ArrayVertexData(ShaderInfo.VertexRequestTagEnum[] requests, float[][] datas, PrimitiveType type = PrimitiveType.Triangles, BufferUsageHint hint = BufferUsageHint.StaticDraw) : base(requests, datas, hint)
        {
            shapeType = type;
            drawSize = datas[0].Length / 3;
        }

        /// <summary>
        /// 构造使用数组绘制的顶点数据对象
        /// </summary>
        /// <param name="requests">数据请求数组</param>
        /// <param name="data">数据内容，混合布局</param>
        /// <param name="type">图元类型</param>
        public ArrayVertexData(ShaderInfo.VertexRequestTagEnum[] requests, float[] data, PrimitiveType type = PrimitiveType.Triangles, BufferUsageHint hint = BufferUsageHint.StaticDraw) : base(requests, data, hint)
        {
            shapeType = type;
            drawSize = data.Length / StrideInFloat();
        }

        override public void Reload(float[] data)
        {
            drawSize = data.Length / StrideInFloat();
            base.Reload(data);
        }

        public override void DrawData()
        {
            GL.BindVertexArray(VAO);
            GL.DrawArrays(shapeType, 0, drawSize);
        }
    }
}
